<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Dashboard extends CI_Controller {

  public function __construct(){
    parent::__construct();

    $this->load->model('model_produk');
    $this->load->model('model_kategori');
    $this->load->model('model_dashboard');

    $this->data['getproduk'] = $this->model_produk->getproduk();
    $this->data['getkategori'] = $this->model_kategori->getAllkategori();
    $this->data['getkota'] = $this->model_dashboard->getkota();
    
  }

  public function index(){

    $this->load->model('model_produk');
    $this->load->model('model_kategori');

    $this->data['getproduk'] = $this->model_produk->getproduk();
    $this->data['getkategori'] = $this->model_kategori->getAllkategori();

    $this->data['page']  = 'home';
    $this->data['title'] = 'Produk';
    $this->load->view('Dashboard', $this->data);
  }

// Kontrol Untuk Pindah Page


  public function carapesan(){

      $this->data['page']  = 'cara_pesan';
      
      $this->load->view('Dashboard', $this->data);
  }

  public function konfirmpesan(){

      $this->data['page']  = 'konfirm_pesan';
      
      $this->load->view('Dashboard', $this->data);
  }

  public function kontak(){

      $this->load->model('model_dashboard');

      $this->data['page']  = 'contact';
      
      $this->load->view('Dashboard', $this->data);
  }

  public function detail($id){

    $this->load->model('model_produk');
    $this->load->model('model_dashboard');

    $this->data['page'] = 'detail_view';

    $this->data['getkomentar'] = $this->model_dashboard->getkomentar();
    $this->data['detail'] = $this->model_produk->getOne($id);

    $this->load->view('Dashboard', $this->data); 
  
    }

  public function pesan($id){
    
    $this->load->model('model_produk');
    $this->load->model('model_dashboard');

    $this->data['page'] = 'pesan';

    $this->data['detail'] = $this->model_produk->getOne($id);
    $this->data['getkota'] = $this->model_dashboard->getkota();

    $this->load->view('Dashboard', $this->data);
  }

  public function about(){

      $this->data['page'] = 'about';
      $this->load->view('Dashboard', $this->data);
  }

  public function produkbykat($id){

    $this->load->model('model_produk');

    $this->data['produkbykat'] = $this->model_produk->produkbykat($id);

    $this->data['page']  = 'produkbykategori';
    $this->data['title'] = 'Produk';
    $this->load->view('Dashboard', $this->data);
  }

// Kontrol untuk tambah pesanan

    public function insertpesanan(){
        $data = array(
                'nama_pemesan' => $this->input->post('namapemesan'),
                'alamat' => $this->input->post('alamat'),
                'telp' => $this->input->post('telp'),
                'email' => $this->input->post('email'),
                'id_kota' => $this->input->post('kota'),
                'nama_produk' => $this->input->post('nama_produk')
              );
          $this->model_dashboard->insertpesanan($data); //passing variable $data ke products_model
          $id   = addslashes($this->input->post('id_produk'));
          redirect('Dashboard/pesan/'.$id);
    }

// Kontrol Untuk tambah Komentar

    public function insertkomentar(){
         $data = array(
                'nama' => $this->input->post('nama'),
                'email' => $this->input->post('email'),
                'url' => $this->input->post('url'),
                'isi_komentar' => $this->input->post('komentar'),
                'tgl' => time()
              );
          $this->model_dashboard->insertkomentar($data); //passing variable $data ke products_model
          $id   = addslashes($this->input->post('id_produk'));
        redirect('Dashboard/detail/'.$id);

    }

// Kontak Us

    public function insertkontak(){
         $data = array(
                'nama' => $this->input->post('nama'),
                'email' => $this->input->post('email'),
                'subjek' => $this->input->post('subjek'),
                'pesan' => $this->input->post('komentar'),
                'tanggal' => time()
              );
          $this->model_dashboard->insertkontak($data); 
          $id   = addslashes($this->input->post('id_produk'));
        redirect('Dashboard/kontak/'.$id);

    }


// Kontrol Untuk Login

  public function admin(){
     $post = $this->input->post('submit');
    if($post){

      $this->load->model('account');

      $this->account->set_user($this->input->post('usr'));
      $this->account->set_password($this->input->post('pwd'));

      $this->form_validation->set_rules('usr', 'Nama Pengguna', 'required|alpha_dash');
      $this->form_validation->set_rules('pwd', 'Kata Sandi', 'required|alpha_dash');

      if ($this->form_validation->run() == FALSE) {
        $data['error'] = validation_errors();     
      }else{

        if($this->account->do_login()){
          header('location: '.site_url().'/admin_panel');
        }else{
          $data['error'] = 'User dan Password tidak sesuai,<br />silahkan periksa kembali';
        } 
      }
    }else{

      $data['error']   = $this->session->flashdata('error');      
    }   

    $this->load->view('login', $data);

  }

}
